package com.orangelife.mobile.parkingcard.adapter;

import java.util.List;
import java.util.Map;

import com.curry.orangelife.mobile.R;
import com.curry.android.adapter.FatherAdapter;
import com.orangelife.mobile.constants.Constant;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PayRecordAdapter<T> extends FatherAdapter<T> {
    private Context context;
    private List<Map<String,Object>> list;
	public PayRecordAdapter(Context context,List<Map<String,Object>> list){
		this.context = context;
		this.list = list;
	}
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("unchecked")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.item_lv_parking_card_fee, null);
			holder.tvPayTime = (TextView) convertView
					.findViewById(R.id.tvPaymentTime);
			holder.tvParkingCard = (TextView) convertView
					.findViewById(R.id.tvParkingCard);
			holder.tvPayMoney = (TextView) convertView
					.findViewById(R.id.tvPaymentMoney);
			holder.tvDiscount = (TextView) convertView
					.findViewById(R.id.tvDiscount);
			holder.tvPeriodValidity = (TextView) convertView
					.findViewById(R.id.tvValidity);
			holder.llParkingCard = (LinearLayout) convertView
					.findViewById(R.id.llParkingCard);
			holder.viewParkingCard = (View) convertView
					.findViewById(R.id.viewParkingCard);
			holder.viewParkingCard.setVisibility(View.VISIBLE);
			holder.llParkingCard.setVisibility(View.VISIBLE);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}
		Map<String,Object> map = list.get(position);
		holder.tvPayTime.setText(map.get("time").toString());
		holder.tvParkingCard.setText(map.get("number").toString());
		holder.tvPayMoney.setText(Constant.RMB_FLAG+map.get("amount").toString());
		double coupon_amount = Double.parseDouble(map.get("coupon_amount").toString());
		if(coupon_amount == 0){
			holder.tvDiscount.setVisibility(View.GONE);
		}else{
			holder.tvDiscount.setVisibility(View.VISIBLE);
			holder.tvDiscount.setText("优惠"+Constant.RMB_FLAG+coupon_amount);
		}
		holder.tvPeriodValidity.setText(map.get("commencing_time")+"至"+map.get("terminating_time").toString());
		return convertView;
	}

	class ViewHolder{
	   /** 缴费时间 */
	   private TextView tvPayTime;
	   /** 停车卡横线*/
	   private View viewParkingCard;
	   /** 停车卡 */
	   private TextView tvParkingCard;
	   /** 停车卡布局*/
	   private LinearLayout llParkingCard;
	   /** 缴费金额 */
	   private TextView tvPayMoney;
	   /** 有效期 */
	   private TextView tvPeriodValidity;
       /** 抵用券金额*/
	   private TextView tvDiscount;
	}

	@SuppressWarnings("unchecked")
	@Override
	public BaseAdapter setList(List<T> list) {
		this.list = (List<Map<String, Object>>) list;
		return PayRecordAdapter.this;
	}
}
